<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<jsp:include page="../../inc.jsp"></jsp:include>
</head>
<script type="text/javascript" charset="utf-8">
	
	var datagrid;//列表对象
	var number = '${param.number}';//判断是否为多选
		
	var bianHao_inputName = '${param.bianHao_inputName}';
	var mingCheng_inputName = '${param.mingCheng_inputName}';
	var single;
	var userCodes2;
	/**
	*加载列表
	**/		
    $(function(){
    	var userCodes = '${userCodes}';
    	userCodes2 = userCodes;
    	loadDatagrid(userCodes);
	});
	function loadUser(userIDs){
		if(userCodes2!=undefined){
			if(userCodes2.length>0){
				loadDatagrid(userIDs+","+userCodes2);
				return false;
			}
		}
		loadDatagrid(userIDs);
	}
	function loadDatagrid(userCodes) {
		userCodes2 = userCodes;
		if(userCodes==undefined||userCodes==''){
			userCodes = 'undefined';
		}
    	$('#selectInput').focus();
    	if(number==1){//单选框
			single = true;
		}else {//多选框
			single = false;
		}
		datagrid = $('#datagrid').datagrid({
			url : 'yongHuAction!find.action?userCodes='+userCodes,//?zhuangTai=11
			title : '',
	//		pagination : true,
	//		pageSize : 10,
	//		pageList : [ 10, 20, 30, 40 ],
			fit : true,
			fitColumns : true,
			nowrap : false,
			border : false,
			idField : 'bianHao',
			singleSelect : single,
			checkOnSelect : false,
			selectOnCheck : true,
			columns : [ [ //字段定义
	   		{
				title : '<s:text name="bianHao"></s:text>',
				field : 'bianHao',
				width : 50,
				sortable : true,
				checkbox : true
			}
			,{
				title : '<s:text name="xingMing"></s:text>',
				field : 'xingMing',
				width : 100
			}
			] ]
			,toolbar : [ //{
			//	text : '选择',
			//	iconCls : 'icon-edit',
			//	handler : function() {
			//		select();
			//	}
			//}, '-',
			 {
				text : '添加',
				iconCls : 'icon-edit',
				handler : function() {
					add();
				}
			}, '-',
			 {
				text : '删除',
				iconCls : 'icon-edit',
				handler : function() {
					shanchu();
				}
			}, '-',
			 {
				text : '确定',
				iconCls : 'icon-edit',
				handler : function() {
					queding();
				}
			}, '-',
			{
				text : '<s:text name="clean"></s:text>',
				iconCls : 'icon-cancel',
				handler : function() {
					clean();
				}
			}, '-']
		});
	}
	function queding(){
		var rows = datagrid.datagrid('getRows');//获取选中行
		var userIDs = '';
		var userNames = '';
		for(var i=0;i<rows.length;i++){
			if(i>0){
				userIDs += ",";
				userNames += ",";
			}
			userIDs += rows[i].bianHao;
			userNames += rows[i].xingMing;
		}
		opener.document.all.userCode.value = userIDs;
		opener.document.all.UserName.value = userNames;
		window.close(); 
	}
	function shanchu(){
		var rows = datagrid.datagrid('getChecked');//获取选中行
		if(rows.length==0){
			alert('请选择记录！');return false;
		}
		for(var i=0;i<rows.length;i++){
			var userid = rows[i].bianHao;
			userCodes2=userCodes2.replace(userid+",");
			userCodes2=userCodes2.replace(userid); 
		}
		loadUser(userCodes2);
	}
	function add(){
		openWin('/ZhongChengLM/yongHuAction!yongHuSelect2.action?number=1&bianHao_inputName=userid&mingCheng_inputName=username','','600','500','no');
	}
	/**
	* 选择
	**/
	function select() {
	try{
		var userID = window.opener.document.getElementById('userID');
		var userName = window.opener.document.getElementById('userName');
	
		if(bianHao_inputName!=undefined&&bianHao_inputName!=''&&bianHao_inputName!=null){
			userID = window.opener.document.getElementById(bianHao_inputName);
		}
		if(mingCheng_inputName!=undefined&&mingCheng_inputName!=''&&mingCheng_inputName!=null){
			userName =window.opener.document.getElementById(mingCheng_inputName);
		}
		var rows = datagrid.datagrid('getChecked');//获取选中行
		if(number==1){
			if(rows.length==1){
				userID.value=rows[0].userID;
				userName.value=rows[0].userName;
				window.close(); 
			} else if (rows.length > 1) {
				alert('<s:text name="PleaseChooseOnlyOneData"></s:text>');
			} else {
				alert('<s:text name="PleaseSelectARecord"></s:text>');
			}
		}
		if(number==2){
			if (rows.length >= 1) {
				var userIDs=userID.value;
				var userNames=userName.value;
				for(var i=0;i<rows.length;i++){
					if(userIDs.length>0){
						userIDs += ",";
						userNames += ",";
					}
					userIDs += rows[i].bianHao;
					userNames += rows[i].xingMing;
				}
				userID.value=userIDs;
				userName.value=userNames;
				window.close(); 
			} else {
				alert('<s:text name="PleaseSelectARecord"></s:text>');
			}
		}
		userName.focus();
		}catch(e){
			alert(e.message);
		}
	}
	
	/**
	* 提交查询
	**/
	function _search() {
		datagrid.datagrid('load', dagong.serializeObject($('#searchForm')));
	}
	
	/**
	* 清空查询条件
	**/
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
		//clean();
	}
	
	/**
	* 清除数据
	**/
	function clean() {
		var userID = window.opener.document.getElementById('userCode');
		var userName = window.opener.document.getElementById('UserName');
		if(bianHao_inputName!=undefined&&bianHao_inputName!=''&&bianHao_inputName!=null){
			userID = window.opener.document.getElementById(bianHao_inputName);
		}
		if(mingCheng_inputName!=undefined&&mingCheng_inputName!=''&&mingCheng_inputName!=null){
			userName =window.opener.document.getElementById(mingCheng_inputName);
		}
		userID.value='';
		userName.value='';
		window.close(); 
	}
</script>
<body>
	<input type="hidden" name="userid" id="userid" onchange="alert(1)"/>
	<input type="hidden" name="username" id="username" onchange="alert(1)"/>
<div class="easyui-panel"style="width:500px;height:200px;padding:0px;background-color: white;" data-options="fit:true,border:false">
	<div class="easyui-layout" data-options="fit:true,border:false">	
		<div data-options="region:'north',border:false" style="height:60px;overflow:hidden;">
			<form id="searchForm" >
			
				<div style="padding:15px; margin: 0px; ">
	               	<span class="title"><s:text name="xingMing"></s:text></span>
	               	<input id="selectInput" type="text" name="xingMing"  class="t_d"/>
	               	<a id="" class="easyui-linkbutton blankSpace" href="javascript:void(0)" onclick="_search();"><s:text name="query"></s:text></a>
	               	<a id="" class="easyui-linkbutton" href="javascript:void(0)" onclick="cleanSearch();">重置</a>
			 	</div>
			
				<!--<div>
					<table class="tableForm datagrid-toolbar" style="width: 100%;height: 100%; padding: 10px;" >
						<tr>
							<td><s:text name="userName"></s:text></td>
							<td><input name="userName" style="width:200px;" /></td>
							<td><s:text name="ZuZhi"></s:text></td>
							<td><input name="zuZhiJGMC" style="width:200px;" /></td>
							<td>&nbsp;</td>
							<td align="center">
								<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();"><s:text name="query"></s:text></a>
								<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();"><s:text name="cancel"></s:text></a>
							</td>
						</tr>
					</table>
				</div>
				-->
			</form>
		</div>
		<div data-options="region:'center',border:false" data-options="fit:true">
			<table id="datagrid"></table>
		</div>
		<!--<div data-options="region:'south',border:false" align="right" data-options="fit:true">
			<a href="javascript:void(0);" class="easyui-linkbutton" onclick="clean();"><s:text name="clean"></s:text></a>
			<a href="javascript:void(0);" class="easyui-linkbutton" onclick="select();"><s:text name="select"></s:text></a>
		</div>
	--></div>
</div>
</body>
</html>
